package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zhaoss
 * @Date: 2023/5/2 - 05 - 02 - 18:08
 * @Description: com.sky.service.impl
 * @version: 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 分页查询菜品套餐
     *
     * @param dto
     * @return
     */
    @Override
    public Result pageSetmeal(SetmealPageQueryDTO dto) {
        if (dto.getPage()==null){
            dto.setPage(0);
        }
        if (dto.getPageSize() == null) {
            dto.setPageSize(5);
        }
        //分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        Page<SetmealVO> page = setmealMapper.page(dto);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        PageResult pageResult = new PageResult(total, result);

        return Result.success(pageResult);
    }

    /**
     * 新增分类
     *
     * @param
     * @return
     */
    @Override
    @Transactional
    public Result addSetmeal(SetmealDTO setmealDTO) {
        //非空判断

        //新增套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSetmeal(setmeal);
        //新增套餐菜品关联表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.addSetmealDish(setmealDishes);

        return Result.success();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result delete(List<Long> ids) {
        if (ids == null||ids.size()==0) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        setmealMapper.delete(ids);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public Result putSetmeal(SetmealDTO setmealDTO) {
        //非空判断
        if (setmealDTO.getId() == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //修改套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.putSetmeal(setmeal);
        //删除关联表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        List<Long>  dishIds = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            Long dishId = setmealDish.getDishId();
            dishIds.add(dishId);
        }

        setmealDishMapper.deleteSetmealDish(dishIds);
        //新增关联表
        setmealDishMapper.addSetmealDish(setmealDishes);

        return Result.success();
    }

    /**
     * id单一查询
     *
     * @param id
     * @return
     */
    @Override
    public Result getById(Long id) {
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);
        Category category = categoryMapper.getById(setmeal.getCategoryId());
        String categoryName = category.getName();


        List<SetmealDish> setmealDishList = setmealDishMapper.getByDishIds(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setCategoryName(categoryName);
        setmealVO.setSetmealDishes(setmealDishList);


        return Result.success(setmealVO);
    }

    /**
     * 修改分类状态
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result putStatus(Integer status, Long id) {
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.putSetmeal(setmeal);
        return Result.success();
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
